package com.zxy.controller;

import com.zxy.pojo.Book;
import com.zxy.service.BookService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class BookController {

    @Autowired
    BookService service;
    @RequestMapping("/success")
    public String getAll(Model model){
        List<Book> list=service.getAll();
        model.addAttribute("list",list);
        return  "success";
    }

    @RequestMapping("/check")
    public void check(){

    }
    @RequestMapping("/add")
    public String AddBook(Book book){
        System.out.println(book);
        int i=service.AddBook(book);
        System.out.println(i);
        return "redirect:/success";
    }

    @RequestMapping("/toUpdate")
    public String toUpdate(Integer BookId,Model model){
        Book book=service.SelectByExample(BookId);
        model.addAttribute("book",book);
        return "update";
    }

    @RequestMapping("/update")
    public String Update(Book book){
        service.UpdateBook(book);
        return "redirect:/success";
    }


    @RequestMapping("/delete")
    public String DeleteBook(Integer BookId){
        Book book=new Book();
        book.setBookId(BookId);
        service.DeleteByExample(book);
        return "redirect:/success";
    }
}
